const db = wx.cloud.database()
let query = ''
Page({
  data: {
    houseList: [],
    houseListByarea: [],
    houseListByrent: [],
    _page: 0,
    hasMore: true,
    tabs: [
      {
        id: 0,
        value: "综合",
        isActive: true
      },
      {
        id: 1,
        value: "租金",
        isActive: false
      },
      {
        id: 2,
        value: "面积",
        isActive: false
      }
    ]
  },
  // 获取房屋列表数据
  getHousesList(query) {
    const LIMIT = 7
    let { _page } = this.data

    db.collection('house_information').limit(LIMIT).skip(_page * LIMIT).where({
      title: db.RegExp({
        regexp: query,
        options: 'i',
      })
    })
      .get()
      .then(res => {
        this.setData({
          houseList: [...this.data.houseList, ...res.data],
          _page: ++_page,
          hasMore: res.data.length === LIMIT
        })
        wx.stopPullDownRefresh();
        wx.hideLoading();
      })
      .catch(res => {
        wx.hideLoading();
        wx.showToast({
          title: '暂无相关数据',
          icon: 'error',
          mask: true
        });
      })
  },
  // 获取房屋列表数据 面积
  getHousesListByarea(query) {
    const LIMIT = 7
    let { _page } = this.data

    db.collection('house_information').orderBy('area', 'desc').limit(LIMIT).skip(_page * LIMIT).where({
      title: db.RegExp({
        regexp: query,
        options: 'i',
      })
    })
      .get()
      .then(res => {
        this.setData({
          houseListByarea: [...this.data.houseListByarea, ...res.data],
          _page: ++_page,
          hasMore: res.data.length === LIMIT
        })
        wx.stopPullDownRefresh();
        wx.hideLoading();
      })
      .catch(res => {
        wx.hideLoading();
        wx.showToast({
          title: '暂无相关数据',
          icon: 'error',
          mask: true
        });
      })
  },
  // 获取房屋列表数据 租金
  getHousesListByrent(query) {
    const LIMIT = 7
    let { _page } = this.data

    db.collection('house_information').orderBy('rent', 'desc').limit(LIMIT).skip(_page * LIMIT).where({
      title: db.RegExp({
        regexp: query,
        options: 'i',
      })
    })
      .get()
      .then(res => {
        this.setData({
          houseListByrent: [...this.data.houseListByrent, ...res.data],
          _page: ++_page,
          hasMore: res.data.length === LIMIT
        })
        wx.stopPullDownRefresh();
        wx.hideLoading();
      })
      .catch(res => {
        wx.hideLoading();
        wx.showToast({
          title: '暂无相关数据',
          icon: 'error',
          mask: true
        });
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var inpValue = options.title
    query = inpValue
    wx.showLoading({
      title: "加载中",
      mask: true
    });
  },

  onShow: function () {
    this.getHousesList(query);
    this.getHousesListByarea(query);
    this.getHousesListByrent(query);
  },

  // 标题点击事件
  handleTabsItemChange(e) {
    const { index } = e.detail;
    let { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
    this.setData({
      tabs
    })
  },

  // 滚动条触底事件
  onReachBottom() {
    if (!this.data.hasMore) {
      wx.showToast({
        title: '暂无更多数据',
        icon: "none"
      });
    } else {
      this.getHousesList(query)
    }
  },

  // 下拉刷新事件
  onPullDownRefresh() {
    this.setData({
      _page: 0,
      houseList: []
    })
    this.getHousesList(query)
  }
})